//
//  SYRefreshControl.m
//  FlagForiOS
//
//  Created by 容贤郁 on 2016/12/10.
//  Copyright © 2016年 Minidai. All rights reserved.
//

#import "SYRefreshControl.h"
#import "MJRefresh.h"

@interface SYRefreshControl ()
//NSArray *idleImages;
//NSArray *pullingImages;
//NSArray *refreshingImages;

@property (strong, nonatomic) NSArray *idleImages;
@property (strong, nonatomic) NSArray *pullingImages;
@property (strong, nonatomic) NSArray *refreshingImages;

@property (copy, nonatomic) void(^refreshBlock)(void);
@property (copy, nonatomic) void(^loadMoreBlock)(void);

@end

@implementation SYRefreshControl

- (instancetype)init {
    self = [super init];
    
    if (self) {
        //此gif为逐帧动画由多张图片组成
        //闲置状态下的gif(就是拖动的时候变化的gif) ***实际使用中要填充图片
        NSMutableArray *images1 = [NSMutableArray array];
        for (NSInteger i = 0; i <= 21; i++) {
            NSString *imageName = [NSString stringWithFormat:@"refresh_%zd", i];
//            NSString *imageName = [NSString stringWithFormat:@"dropdown_anim__000%zd", i];
            [images1 addObject:[UIImage imageNamed: imageName]];
        }
        
        NSMutableArray *images2 = [NSMutableArray array];
        for (NSInteger i = 22; i <= 29; i++) {
            NSString *imageName = [NSString stringWithFormat:@"refresh_%zd", i];
//            NSString *imageName = [NSString stringWithFormat:@"dropdown_loading_0%zd", i];
            [images2 addObject:[UIImage imageNamed:imageName]];
        }
        self.idleImages = [NSArray arrayWithArray:images1];    //@[[UIImage imageNamed:@"...."]];
        self.pullingImages = [NSArray arrayWithArray:images2]; //@[[UIImage imageNamed:@"...."]];
        self.refreshingImages = [NSArray arrayWithArray:images2]; //@[[UIImage imageNamed:@"...."]];
    }
    return self;
}

+ (void)configNormalMJRefresh:(UIScrollView *)scrollView
                  refreshType:(MJRefreshSupport)refreshType
                 beginRefresh:(BOOL)beginRefresh
              timeLabelHidden:(BOOL)timeLabelHidden
             stateLabelHidden:(BOOL)stateLabelHidden
                 refreshBlock:(void (^)(void))refreshBlock
                loadMoreBlock:(void (^)(void))loadMoreBlock {
    //初始化
    SYRefreshControl *refreshControl = [[SYRefreshControl alloc] init];
    
    switch (refreshType) {
        case MJRefreshSupportALl:
        {
            //配置header
            [self configNormalHeader:refreshControl
                          scrollView:scrollView
                        beginRefresh:beginRefresh
                     timeLabelHidden:timeLabelHidden
                    stateLabelHidden:stateLabelHidden
                        refreshBlock:refreshBlock];
            //配置footer
            [self configFooter:refreshControl
                    scrollView:scrollView
                 loadMoreBlock:loadMoreBlock];
        }
            break;
            
        case MJRefreshSupportNone:
        {
            scrollView.mj_header = nil;
            scrollView.mj_footer = nil;
        }
            break;
        case MJRefreshSupportRefresh:
        {
            scrollView.mj_footer = nil;
            [self configNormalHeader:refreshControl
                          scrollView:scrollView
                        beginRefresh:beginRefresh
                     timeLabelHidden:timeLabelHidden
                    stateLabelHidden:stateLabelHidden
                        refreshBlock:refreshBlock];
            
        }
            break;
        case MJRefreshSupportLoadMore:
        {
            scrollView.mj_header = nil;
            //配置footer
            [self configFooter:refreshControl
                    scrollView:scrollView
                 loadMoreBlock:loadMoreBlock];
        }
            break;
        default:
            break;
    }
}

+ (void)configGifMJRefresh:(UIScrollView *)scrollView
               refreshType:(MJRefreshSupport)refreshType
              beginRefresh:(BOOL)beginRefresh
           timeLabelHidden:(BOOL)timeLabelHidden
          stateLabelHidden:(BOOL)stateLabelHidden
              refreshBlock:(void (^)(void))refreshBlock
             loadMoreBlock:(void (^)(void))loadMoreBlock {
    
    //初始化
    SYRefreshControl *refreshControl = [[SYRefreshControl alloc] init];
    
    switch (refreshType) {
        case MJRefreshSupportALl:
        {
            //配置header
            [self configGifHeader:refreshControl
                       scrollView:scrollView
                     beginRefresh:beginRefresh
                  timeLabelHidden:timeLabelHidden
                 stateLabelHidden:stateLabelHidden
                     refreshBlock:refreshBlock];
            
            //配置footer
            [self configFooter:refreshControl
                    scrollView:scrollView
                 loadMoreBlock:loadMoreBlock];
        }
            break;
        case MJRefreshSupportNone:
        {
            scrollView.mj_header = nil;
            scrollView.mj_footer = nil;
        }
            break;
        case MJRefreshSupportRefresh:
        {
            scrollView.mj_footer = nil;
            
            [self configGifHeader:refreshControl
                       scrollView:scrollView
                     beginRefresh:beginRefresh
                  timeLabelHidden:timeLabelHidden
                 stateLabelHidden:stateLabelHidden
                     refreshBlock:refreshBlock];
        }
            break;
        case MJRefreshSupportLoadMore:
        {
            scrollView.mj_header = nil;
            
            [self configFooter:refreshControl scrollView:scrollView loadMoreBlock:loadMoreBlock];
        }
            break;
            
        default:
            break;
    }
}

+ (void)configNormalHeader:(SYRefreshControl *)refreshControl
                scrollView:(UIScrollView *)scrollView
              beginRefresh:(BOOL)beginRefresh
           timeLabelHidden:(BOOL)timeLabelHidden
          stateLabelHidden:(BOOL)stateLabelHidden
              refreshBlock:(void (^)(void))refreshBlock {
    //配置header
    refreshControl.refreshBlock = refreshBlock;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (refreshControl.refreshBlock) {
            refreshControl.refreshBlock();
        }
    }];
    header.lastUpdatedTimeLabel.hidden = timeLabelHidden;
    header.stateLabel.hidden = stateLabelHidden;
    scrollView.mj_header = header;
    if (beginRefresh) {
        [scrollView.mj_header beginRefreshing];
    }
    scrollView.mj_header.automaticallyChangeAlpha = YES;
}

+ (void)configGifHeader:(SYRefreshControl *)refreshControl
             scrollView:(UIScrollView *)scrollView
           beginRefresh:(BOOL)beginRefresh
        timeLabelHidden:(BOOL)timeLabelHidden
       stateLabelHidden:(BOOL)stateLabelHidden
           refreshBlock:(void (^)(void))refreshBlock {

//    NSAssert(refreshControl.idleImages.count != 0, @"请在init方法里为idleImages配置GIF图片");
//    NSAssert(refreshControl.pullingImages.count != 0, @"请在init方法里为pullingImages配置GIF图片");
//    NSAssert(refreshControl.refreshingImages.count != 0, @"请在init方法里为refreshingImages配置GIF图片");
    //配置header
    refreshControl.refreshBlock = refreshBlock;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        if (refreshControl.refreshBlock) {
            refreshControl.refreshBlock();
        }
    }];

    [header setImages:refreshControl.idleImages forState:MJRefreshStateIdle];
    [header setImages:refreshControl.pullingImages forState:MJRefreshStatePulling];
//    [header setImages:refreshControl.refreshingImages duration: 1.6 forState:MJRefreshStateRefreshing];
    [header setImages:refreshControl.refreshingImages forState:MJRefreshStateRefreshing];
    
    header.lastUpdatedTimeLabel.hidden = timeLabelHidden;
    header.stateLabel.hidden = stateLabelHidden;
    scrollView.mj_header = header;
    if (beginRefresh) {
        [scrollView.mj_header beginRefreshing];
    }
    scrollView.mj_header.automaticallyChangeAlpha = YES;
}

+ (void)configFooter:(SYRefreshControl *)refreshControl
          scrollView:(UIScrollView *)scrollView
       loadMoreBlock:(void (^)(void))loadMoreBlock  {
    //配置footer
    refreshControl.loadMoreBlock = loadMoreBlock;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (refreshControl.loadMoreBlock) {
            refreshControl.loadMoreBlock();
        }
    }];
//    footer.automaticallyHidden = YES;
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    scrollView.mj_footer = footer;
}

+ (void)configAfterLoading:(UIScrollView *)scrollView hasData:(BOOL)hasData {
    if (scrollView.mj_header.isRefreshing) {
        [scrollView.mj_header endRefreshing];
    }
    if (hasData) {
        [scrollView.mj_footer resetNoMoreData];
        [(MJRefreshAutoStateFooter *)scrollView.mj_footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    } else {
        [scrollView.mj_footer endRefreshingWithNoMoreData];
        __weak typeof(self) weakSelf = self;
        [self bk_performBlock:^{
            [weakSelf hiddenNoMoreDataFooter:(MJRefreshAutoStateFooter *)scrollView.mj_footer];
            
        } afterDelay:1.5];
    }
}

+ (void)hiddenNoMoreDataFooter:(MJRefreshAutoStateFooter *)footer {
    [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
}
@end
